package com.weavernorth.salary;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 此action是店铺销售提成对比月任务,正价销售汇总,折价销售 汇总,给出提示
 */
public class CheckRepeatUserAction extends BaseBean implements Action {

    public static Log log = LogFactory.getLog(CheckRepeatUserAction.class.getName());

    /**
     * 销售提成流程判断选择的人员是否重复
     */
    @Override
    public String execute(RequestInfo request) {
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsSelect = new RecordSet();
        //当前流程id
        String strRequestid = Util.null2String(request.getRequestid());
        //当前表名
        String strTable = Util.null2String(request.getRequestManager().getBillTableName());

        //查询当前流程明细的数据
        String strCurrSql = "select fmd.id,fmd.ygxm from " + strTable + " fm," + strTable + "_dt1 fmd where fm.id=fmd.mainid and fm.requestid=?";
        log.info("查询当前流程的明细数据:" + strCurrSql + ",requestid:" + strRequestid);
        rsSelect.executeQuery(strCurrSql, strRequestid);
        while (rsSelect.next()) {
            String strCurrYgxm = Util.null2String(rsSelect.getString("ygxm"));
            String strId = Util.null2String(rsSelect.getString("id"));
            if (!"".equals(strCurrYgxm)) {
                String[] strArrCurrYgxm = strCurrYgxm.split(",");

                //拼接字符串
                StringBuffer strbuUser = new StringBuffer();

                //提示信息
                String strMess = "";
                //查询所有已存的所有已提交和本条流程数据
                String strSql = "select ygxm from " + strTable + " fm," + strTable + "_dt1 fmd,workflow_requestbase wr where fm.requestid=wr.requestid and " +
                        " fm.id=fmd.mainid and (wr.currentnodetype!='0' or fm.requestid=?) and substr(fm.czrq, 0, 7) = (select substr(czrq, 0, 7)    from " + strTable + " where requestid = ?) and fmd.id!=?";
                log.info("查询员工是否重复sql:" + strSql + ",requestid:" + strRequestid);
                rs.executeQuery(strSql, strRequestid,strRequestid,strId);
                while (rs.next()) {
                    strbuUser.append(Util.null2String(rs.getString("ygxm"))).append(",");

                }
                log.info("最终拼接的所有姓名字段:" + strbuUser.toString());
                String[] strUser = strbuUser.toString().split(",");
                String strTemp = "";
                for (int i = 0; i < strArrCurrYgxm.length; i++) {
                    strTemp = strArrCurrYgxm[i];
                    for (int j = 0; j < strUser.length; j++) {
                        if (strTemp.equals(strUser[j])) {
                            log.info("重复的人员id:" + strTemp);

                            strMess = "本月,员工为:" + getUserName(strTemp) + "不允许重复选择,请重新选择!";
                            request.getRequestManager().setMessagecontent(strMess);
                            return FAILURE_AND_CONTINUE;
                        }
                    }
                }
            }
        }

        return SUCCESS;
    }

    /**
     * 根据员工id得到员工姓名
     */

    public String getUserName(String strUserid) {
        RecordSet rs = new RecordSet();
        String strUsername = "";
        rs.executeQuery("select lastname from hrmresource where id=?", strUserid);
        if (rs.next()) {
            strUsername = Util.null2String(rs.getString("lastname"));
        }
        return strUsername;

    }
}
